$(function () {

    var pageNum = 1;
    var pageSize = 5;
    var userId = Cookies.get("userId");

    $.ajax({
        type: "get",
        url: "/work/getJobList?pageNum=" + pageNum + "&pageSize=" + pageSize + "&userId=" + userId,
        dataType: "json",
        success: function (result) {
            // 生成列表
            generateHtml(result);

            // 生成页码
            generatePage(result);
        },
        error: function () {
            $("#warning-fade").html("服务请求失败！");
            $("#myModal").modal();
        }
    });

    // 生成工作列表
    function generateHtml(result) {
        var html = '';
        $.each(result.data.data, function (i, item) {
            var div = '<div class="col-lg-9 mt-4 pt-2">' +
                '<div class="job-list-box border rounded">' +
                '<div class="p-3">' +
                '<div class="row align-items-center">' +
                '<div class="col-lg-2">' +
                '<div class="company-logo-img">' +
                '<img src="" alt="" class="img-fluid mx-auto d-block">' +
                '</div>' +
                '</div>' +
                '<div class="col-lg-7 col-md-9">' +
                '<div class="job-list-desc">' +
                '<h6 class="mb-2"><a href="job-details.html?jobId='+item.jobId+'" class="text-dark">' + item.jobTitle + '</a></h6>' +
                '<p class="text-muted mb-0"><i class="mdi mdi-bank mr-2"></i>部门：' + item.department + '</p>' +
                '<ul class="list-inline mb-0">' +
                '<li class="list-inline-item mr-3">' +
                '<p class="text-muted mb-0"><i class="mdi mdi-cellphone mr-2"></i>电话：' + item.phoneNumber + '</p>' +
                '</li>' +
                '<li class="list-inline-item">' +
                '<p class="text-muted mb-0"><i class="mdi mdi-clock-outline mr-2"></i>' + item.gmtCreate + '</p>' +
                '</li>' +
                '</ul>' +
                '</div>' +
                '</div>' +
                '<div class="col-lg-3 col-md-3">' +
                '<div class="job-list-button-sm text-right">' +
                '<div class="mt-3">' +
                '<a href="job-details.html?jobId=' + item.jobId + '" class="btn btn-sm btn-primary">详情</a>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            html = html + div;
        });
        html = html + '<input id="page_size" value="'+result.pageSize+'" type="text" name="name" class="form-control resume" hidden>';
        html = html + '<input id="page_num" value="'+result.pageNum+'" type="text" name="name" class="form-control resume" hidden>';

        var div_obj = $("#job-list-div");
        div_obj.html("");
        div_obj.append(html);
    }

    // 生成页码
    function generatePage(result) {
        Cookies.set('pageNum', result.data.pageNum, {expires: 7, path: ''});
        var page_li_obj = $("#page-ul");
        page_li_obj.html('');
        var total = result.data.total;
        var page_html = '';
        var pre_page_html = '<li class="page-item disabled">' +
            '<a class="page-link" href="" tabindex="-1" aria-disabled="true">' +
            '<i class="mdi mdi-chevron-double-left f-15"></i>' +
            '</a>' +
            '</li>';
        page_html = page_html + pre_page_html;

        // 向上取整
        var num = Math.ceil(total / pageSize);
        for (var i = 1; i <= num; i++) {
            var page_li = '<li class="page-item" id="li_'+i+'"><a class="page-link" onclick="click_page_num('+i+')">'+i+'</a></li>';
            if(pageNum == i){
                page_li = '<li class="page-item active" id="li_'+i+'"><a class="page-link" onclick="click_page_num('+i+')">'+i+'</a></li>';
            }
            page_html = page_html + page_li;
        }
        var next_page_html = '<li class="page-item">' +
            '<a class="page-link" onclick="next()">' +
            '<i class="mdi mdi-chevron-double-right f-15"></i>' +
            '</a>' +
            '</li>';
        page_html = page_html + next_page_html;
        page_li_obj.append(page_html);
    }

    // 判断筛选条件是否改变
    $('input[type=radio][name=customRadio]').change(function () {
        var userId = Cookies.get("userId");
        var date = 1;
        if (this.id == 'custom-radio1') {
            date = 1;
        } else if (this.id == 'custom-radio2') {
            date = 2;
        } else if (this.id == 'custom-radio3') {
            date = 3;
        } else if (this.id == 'custom-radio4') {
            date = 4;
        } else if (this.id == 'custom-radio5') {
            date = 5;
        }
        $.ajax({
            type: "get",
            url: "/work/getJobList?pageNum=" + pageNum + "&pageSize=" + pageSize + "&userId=" + userId + "&date=" + date,
            dataType: "json",
            success: function (result) {
                generateHtml(result);
                // 生成页码
                generatePage(result);
            },
            error: function () {
                $("#warning-fade").html("服务请求失败！");
                $("#myModal").modal();
            }
        });
    });
});